package auctionhaus

import org.junit.*
import grails.test.mixin.*
import grails.plugins.springsecurity.SpringSecurityService

@TestFor(CustomerController)
@Mock([Customer, Bid, Listing, CustomerCreateService,SpringSecurityService])
class CustomerControllerTests {

    Customer seller     //the seller for the listing
    Customer bidder  //the first bidder for the listing

    @Before
    void setUp() {
        //seed database with test data
        seller = new Customer(email: "kleczka@gmail.com",password: "123456", enabled: true)
        assert seller.validate()
        seller.save(flush: true)

        bidder = new Customer(email: "adambackstrom@gmail.com",password: "7891011", enabled: true)
        assert bidder.validate()
        bidder.save(flush: true)

        //The listing has a starting price of 1.0 and and end date 1 day in the future
        Listing listing = new Listing(name: "listing1",
                endDateTime: new Date() + 1,
                startingBidPrice: 1.0f,
                seller: seller)
        assert listing.validate()
        listing.save(flush: true)

        Bid bid = new Bid(amount: 1.0f,
                bidDateTime: new Date(),
                bidder: bidder)
        listing.addToBids(bid)
        listing.save(flush: true)
    }

    def populateValidParams(params) {
        assert params != null
        params["email"] = "kleczka@yahoo.com"
        params["password"] = "1234567"
    }
    
    void testValidDelete() {
        controller.delete()
        assert flash.message != null
        assert response.redirectedUrl == '/customer/list'

        response.reset()

        def c = Customer.count
        
        populateValidParams(params)
        def customer = new Customer(params)

        assert customer.save() != null
        assert customer.errors.errorCount == 0

        params.id = customer.id

        controller.delete()

        assert Customer.count() == c
        assert Customer.get(customer.id) == null
        assert response.redirectedUrl == '/customer/list'
    }

    void testDeleteWithBids()
    {
        params.id = bidder.id

        controller.delete()

        assert Customer.get(bidder.id) != null
        println flash.message
        assert flash.message == 'customer.still.has.bids.message'
    }

    void testDeleteWithListings()
    {
        params.id = seller.id

        controller.delete()

        assert Customer.get(seller.id) != null
        assert flash.message == 'customer.still.has.listings.message'
    }

    void testCreateCustomer()
    {
        params.email = "kleczka@abc.com"
        params.password = "1234567"
        
        controller.save()
        
        assert Customer.findByEmail("kleczka@abc.com")
        assert flash.message == 'default.created.message'
    }

    void testCreateCustomerBadEmail()
    {
        params.email = "kleczka"
        params.password = "1"

        controller.save()

        assert !Customer.findByEmail("kleczka")
        //assert ((Customer)(model.customerInstance)).errors['email'].code == "email.invalid"
    }

    void testCreateCustomerBadPasword()
    {
        params.email = "kleczka@abc.com"
        params.password = "1"

        controller.save()

        assert !Customer.findByEmail("kleczka@abc.com")
        //assert ((Customer)(model.customerInstance)).errors['password'].code == "minSize.notmet"
    }

    void testCreateCustomerExistingEmail()
    {
        params.email = "kleczka@gmail.com"
        params.password = "1234567"
        
        controller.save()

        assert ((Customer)(model.customerInstance)).errors['email'].code == "unique"
    }
}
